package com.kfirszt.elearning.persistence.dao;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.kfirszt.elearning.persistence.model.BaseEntity;

public abstract class GenericDAOImpl<T extends BaseEntity, K extends Serializable>
		implements GenericDAO<T, K> {
	
	protected Class<T> entityClass;
	
	@Autowired
	HibernateTemplate hibernateTemplate;

	@Override
	public T findById(K id) {
		return hibernateTemplate.get(getEntityClass(), id);
	}

	@Override
	public List<T> findAll() {
		return hibernateTemplate.loadAll(getEntityClass());
	}

	@SuppressWarnings("unchecked")
	@Override
	public K create(T entity) {
		return (K) hibernateTemplate.save(entity);
	}

	@Override
	public void merge(T entity) {
		hibernateTemplate.merge(entity);
		
	}

	@Override
	public void delete(T entity) {
		hibernateTemplate.delete(entity);
	}
	
	public void saveOrUpdate(T entity) {
		hibernateTemplate.saveOrUpdate(entity);
	};

	public abstract Class<T> getEntityClass();
}
